﻿using System;
using System.Data;
using System.Linq;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

//BY PJ     2009-05-31
//Cookie Cookie = new Cookie();
//Cookie.setCookie("name", "aaa",1);//赋值
//Cookie.getCookie("name");//取值
//Cookie.delCookie("name");//删除
//Cookie.getEmpInfo();如果cookie中不存在empid，返回空值
//注意:当Cookie存中文出现乱码,则在存放时给中文编码,如Cookie.setCookie("name", Server.UrlEncode("aaa"),1),读取时解码即可
//另外：只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效 

namespace StoragePeriod.COMP
{
    public static class Cookie
    {

        #region 基础方法
        /// <summary>
        /// Cookies赋值
        /// </summary>
        /// <param name="strName">主键</param>
        /// <param name="strValue">键值</param>
        /// <param name="strDay">有效天数</param>
        /// <returns></returns>
        public static bool setCookie(string strName, string strValue, int strDay)
        {
            try
            {                
                HttpCookie Cookie = new HttpCookie(strName);
                //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
                Cookie.Expires = DateTime.Now.AddDays(strDay);
                Cookie.Value = strValue;
                Cookie.Path="/";
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 读取Cookies
        /// </summary>
        /// <param name="strName">主键</param>
        /// <returns></returns>

        public static string getCookie(string strName)
        {
            HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (Cookie != null)
            {
                return Cookie.Value.ToString();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除Cookies
        /// </summary>
        /// <param name="strName">主键</param>
        /// <returns></returns>
        public static bool delCookie(string strName)
        {
            try
            {
                if (HttpContext.Current.Request.Cookies[strName] != null)
                {
                    HttpCookie Cookie = new HttpCookie(strName);
                    //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
                    Cookie.Expires = DateTime.Now.AddDays(-1d);
                    System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                    //System.Web.HttpContext.Current.Response.Cookies["roleCheck"].Value = "";
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

    } 
}